#ifndef __DETECT_H__
#define __DETECT_H__
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <stdint.h>
#include <string>
#include <sys/time.h>
template <typename T> class DetectTask {
    public:
        DetectTask () {
            is_run_complete_ = false;
        }
        virtual ~DetectTask () = default;
    virtual void Run (const T &input,T &out) = 0;
        void      signal () {
            {
                std::lock_guard<std::mutex> lock (mtx_);
                is_run_complete_ = true;
            }
            cv_.notify_one ();
        }
        void wait () {
            std::unique_lock<std::mutex> lock (mtx_);
            cv_.wait (lock, [this] { return is_run_complete_; });
            is_run_complete_ = false;
        }

        void forward (const T &input, T &output, bool async = false) {
            std::cout << name << "Run start ..." << std::endl;
            int64_t start = getSysTime ();
            Run (input,output);
            int64_t diff = getSysTime () - start;
            std::cout << name << "Run end " << diff << "us" << std::endl;
        }

        T getResult () {
            return out;
        };

    protected:
        std::string name;
        int64_t     getSysTime () {
            struct timeval tv;
            gettimeofday (&tv, NULL);
            return tv.tv_sec * 1000 * 1000LL + tv.tv_usec;
        }

    private:
        std::mutex              mtx_;
        std::condition_variable cv_;
        bool                    is_run_complete_;
        T                       out;
};
#endif
